
package com.jf.cloud.multishop.controller.app;

import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.common.constant.StatusEnum;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.response.ResponseEnum;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.multishop.constant.NoticeType;
import com.jf.cloud.multishop.dto.NoticeDTO;
import com.jf.cloud.multishop.service.NoticeService;
import com.jf.cloud.multishop.vo.NoticeVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 公告
 *
 * @author zz
 * @date 2020-11-25 17:57:56
 */
@RestController("appNoticeController")
@RequestMapping("/ma/notice")
@Tag(name = "app-公告")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping("/list")
    @Operation(summary = "获取公告列表" , description = "获取公告列表")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<List<NoticeVO>> list(@ParameterObject @RequestParam("shopId") Long shopId) {
        List<NoticeVO> notices;
        List<NoticeVO> result ;
        Long accountId ;
        UserInfoInTokenBO userInfoInTokenBO = AuthUserContext.get();
        notices = noticeService.getlistByShopId(shopId, NoticeType.TO_USER.value());
        //筛选出立即发送和到时间定时发送的公告
        List<NoticeVO> list = notices.stream().filter(noticeVO -> Objects.equals(noticeVO.getImmediatelySend(), 1) || (Objects.equals(noticeVO.getImmediatelySend(), 0) && noticeVO.getSendTime().before(new Date()))).collect(Collectors.toList());
        //登录状态显示 指定全部用户和指定用户可见的公告
        if(Objects.nonNull(userInfoInTokenBO)){
            accountId = AuthUserContext.get().getUserId();
            String userId = String.valueOf(accountId);
            result = list.stream().filter(notice ->Objects.isNull(notice.getUserIds()) || (Objects.nonNull(notice.getUserIds()) && notice.getUserIds().contains(userId) )).collect(Collectors.toList());
        }else{
            //未登录状态只显示 全部用户可见的公告
            result = list.stream().filter(notice -> Objects.isNull(notice.getUserIds())).collect(Collectors.toList());
        }

        return ServerResponseEntity.success(result);
    }

    @GetMapping
    @Operation(summary = "获取公告分页列表" , description = "获取公告分页列表")
    @Parameter(name = "shopId", description = "店铺id")
    public ServerResponseEntity<PageVO<NoticeVO>> page(@Valid PageDTO pageDTO, @ParameterObject @RequestParam("shopId") Long shopId) {
        NoticeDTO noticeDTO = new NoticeDTO();
        noticeDTO.setShopId(shopId);
        noticeDTO.setType(NoticeType.TO_USER.value());
        noticeDTO.setStatus(StatusEnum.ENABLE.value());
        noticeDTO.setSendTime(new Date());
        noticeDTO.setAccountId(Objects.nonNull(AuthUserContext.get()) ? AuthUserContext.get().getUserId() : null);
        PageVO<NoticeVO> noticePage = noticeService.pagePlatformNotice(pageDTO, noticeDTO);
        return ServerResponseEntity.success(noticePage);
    }

    @GetMapping("/info")
    @Operation(summary = "获取公告" , description = "根据id获取公告并更新为已读状态")
    public ServerResponseEntity<NoticeVO> getById(@ParameterObject @RequestParam Long id) {
        noticeService.updateIsLearning(id);
        NoticeVO noticeVO = noticeService.getById(id);
        if (Objects.isNull(noticeVO)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        List<Integer> typeList = Arrays.stream(noticeVO.getTypes().split(",")).map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        for ( Integer type : typeList) {
            noticeService.removeCacheByShopIdAndType(noticeVO.getShopId(), type);
        }
        return ServerResponseEntity.success(noticeVO);
    }

}
